$(function () {
    //初始化专业
    $.initSubject({
        element:'book-open-subject-box',//初始化专业select元素的ID
        context:'book-open-subject-context',//显示专业/科目名字内容ID
        initData:$('#subjectIds').val(),//初始化显示的专业数据，默认不设置
        removeCallback:function(parentId,subjectIds){//删除回调
            resetSubjectIds()
        },
        confirmCallback:function(parentId,subjectIds){//点击确认回调
            resetSubjectIds()
        }
    });
    /**
     * 重置专业ID串
     */
    function resetSubjectIds() {
        var _subjectIds = $.getAllSubjectIds("book-open-subject-context");
        $("input[name=subjectIds]").val(_subjectIds);
    }

    initLayerDate("#publishTime",false);
    //初始化编辑框
    initSNU({
        id:'depict',
        type:1,
        toolsArr:null,
        height:360,
        gs:true,
        video:true
    });
    //初始化编辑框
    initSNU({
        id:'contents',
        type:1,
        toolsArr:null,
        height:360,
        gs:true,
        video:true
    });
    $("body").on('click','#uploadImage',function () {
        //初始化图片上传窗口
        initUpdateFileWin({
            fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
            callback:'uploadBookImageCallback',
            cut:true,//开启剪切
            imgSize:DEFAULT_BOOK_IMG_SIZE,//剪切上传后生成图片的规格（开启剪切才起使用）
            aspectRatio:2/3//剪切区域的高宽比例（开启剪切才起使用）
        });
    }).on('blur','.num-bx',function () {//监听验证整数
        var val = this.value;
        if(!/^[0-9]+$/.test(val)){
            this.value=0;
        }
    }).on('blur','.price-box',function () {
        var val = this.value;
        if(!/^[0-9]+(.[0-9]{1,2})?$/.test(val)){
            this.value=0;
        }
    }).on('click','#saveReturn',function () {
        saveBook(1);
    }).on('click','#savePublish',function () {
        saveBook(2);
    }).on('click','#saveCancel',function () {//取消
        window.history.go(-1);
    }).on('click','#remove-image',function () {
        removePageImg();
    });

    /**
     * 保存图书
     * @param type 1保存返回，2保存并上架
     */
    function saveBook(type) {
        $(".save-btn").attr('disabled','disabled');
        var params = $("#bookForm").serializeJSON();
        params = JSON.stringify(params);
        $.ajax({
            type:'post',
            url:baseloaction +'/admin/book/saveBook?type='+type,
            dataType:'json',
            data:params,
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                if(result.success==true){
                    window.location.href = baseloaction +'/admin/book/bookList';
                }else{
                    $(".save-btn").removeAttr('disabled');
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                $(".save-btn").removeAttr('disabled');
                icModale(0,'系统错误，保存图书数据失败');
            }
        });
    }
});

/**
 * 图片上传回调
 * @param result
 */
function uploadBookImageCallback(result) {
    if(result!=null){
        $("input[name='imageJson']").val(encodeURIComponent(JSON.stringify(result)));
        $("#material-image").attr("src",result.pcUrlMap.large);
        $("#file-names").text(result.fileName);
        $("#create-time").text(result.createTime);
    }
}